<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="btn" uri="http://www.dsdl.com/dev/jsp/btn" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<%@include file="../base/base.jspf" %>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="usrUsername" placeholder="请输入账号" autocomplete="off" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="usrName" placeholder="请输入姓名" autocomplete="off" class="layui-input"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="usrMobile" placeholder="请输入联系电话" autocomplete="off"
                               class="layui-input"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="usrRoleName" placeholder="请输入角色名称" autocomplete="off"
                               class="layui-input"/>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="usrStt">
                            <option value="0">正常</option>
                            <option value="1">删除</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <script type="text/html" id="toolbar-userListPage-user">
                <div class="layui-btn-container">
                    <btn:btn btnStr="userAdd">
                        <button class="layui-btn" lay-event="add">新增</button>
                    </btn:btn>
                </div>
            </script>
            <table id="user-page-data" lay-filter="user-page-data" class="layui-hide"></table>
            <script type="text/html" id="tool-userListPage-user">
                <btn:btn btnStr="passwordReset">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="reset"><i
                            class="layui-icon layui-icon-refresh"></i>重置密码</a>
                </btn:btn>
                <btn:btn btnStr="userEdit">
                    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit"><i
                            class="layui-icon layui-icon-edit"></i>编辑</a>
                </btn:btn>
                {{# if(d.usrStt == 0 ){ }}
                <btn:btn btnStr="userDelete">
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                            class="layui-icon layui-icon-delete"></i>删除</a>
                </btn:btn>
                {{# } }}
                {{# if(d.usrStt == 1 ){ }}
                <btn:btn btnStr="userRecover">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="recover"><i
                            class="layui-icon layui-icon-refresh"></i>恢复</a>
                </btn:btn>
                {{# } }}
            </script>
        </div>
    </div>
</div>
<script type="text/javascript">
    layui.config({
        base: "${ctx}/static/framework/layuiadmin/" //静态资源所在路径
    }).extend({
        index: "lib/index" //主入口模块
    }).use(["index", "table", "form"], function () {
        var table = layui.table, form = layui.form;
        table.render({
            elem: "#user-page-data",
            url: "${ctx}/user/getUserList",
            method: "post",
            toolbar: "#toolbar-userListPage-user",
            defaultToolbar: ["filter", "print"],
            height: "full-170",//自己根据表格上方和下方的高度减去对应数值，保证侧边无滚动条即可
            cols: [[{
                field: "usrAssociationId",
                title: "协会",
                sort: true,
                width:100,
                templet: function (data) {
                    return xiehuiTrans(data.usrAssociationId);
                }
            }, {
                field: "usrUsername",
                title: "账号",
                align: "center",
                width: 150,
                sort: true
            }, {
                field: "usrName",
                title: "姓名",
                width: 120,
                align: "center"
            }, {
                field: "usrBirthday",
                title: "生日",
                width: 180,
                align: "center",
                templet: function (data) {
                    if(data.usrBirthday != null && data.usrBirthday != ""){
                        return parseToDate(data.usrBirthday).format("yyyy-MM-dd");
                    }else{
                        return "";
                    }
                }
            }, {
                field: "usrMobile",
                title: "联系电话",
                width: 150,
                align: "center"
            }, {
                field: "usrRoleName",
                title: "角色名称",
                width: 150,
                align: "center"
            }, {
                field: "usrCreateTime",
                title: "创建时间",
                align: "center",
                width: 180,
                sort: true,
                templet: function (data) {
                    if(data.usrCreateTime != null && data.usrCreateTime != ""){
                        return parseToDate(data.usrCreateTime).format("yyyy-MM-dd");
                    }else{
                        return "";
                    }
                }
            }, {
                title: "操作",
                width: 300,
                align: "center",
                fixed: "right",
                toolbar: "#tool-userListPage-user"
            }]],
            page: true,
            limit: 20,
            limits: [20, 50, 100],
            text: {
                none: "暂无相关数据"
            }
        });

        form.on("submit(user-front-search)", function (data) {
            table.reload("user-page-data", {
                where: data.field,
                page: {curr: 1}
            });
        });

        table.on("tool(user-page-data)", function (obj) {
            var data = obj.data;
            if (obj.event == "reset") {
                layer.confirm("确定将“" + data.userName + "”的密码重置为\"123456\"？", {
                    icon: 3,
                    title: "提示",
                    btn: ["确定", "取消"] //按钮
                }, function (index) {
                    $.ajax({
                        async: true,
                        url: "${ctx}/user/passwordReset",
                        type: "post",
                        dataType: "json",
                        data: {
                            id: data.usrId
                        },
                        success: function (data) {
                            if (data.result) {
                                success(data.info);
                            } else {
                                error(data.info);
                            }
                        }
                    });
                    layer.close(index);
                }, function (index) {
                    layer.close(index);
                });
            } else if (obj.event == "del") {
                layer.confirm("您确定要删除该用户吗？", {
                    icon: 3,
                    title: "提示",
                    btn: ["确定", "取消"] //按钮
                }, function (index) {
                    tip_index = load();
                    $.ajax({
                        async: true,
                        url: "${ctx}/user/userDelete",
                        type: "post",
                        dataType: "json",
                        data: {
                            id: data.usrId
                        },
                        success: function (data) {
                            layer.close(tip_index);
                            if (data.result) {
                                success(data.info);
                                table.reload("user-page-data");
                            } else {
                                error(data.info);
                            }
                        }
                    });
                    layer.close(index);
                }, function (index) {
                    layer.close(index);
                });
            } else if (obj.event == "edit") {
                var data = obj.data;
                layer.open({
                    type: 2,
                    title: "用户修改",
                    content: "${ctx}/user/userSubmitPage?id=" + data.usrId,
                    offset: "t",
                    area: ["850px", "700px"],
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        layero.find("iframe").contents().find("#user-front-submit").trigger("click");
                    }
                });
            }else if(obj.event == "recover"){
                layer.confirm("您确定要恢复该用户吗？", {
                    icon: 3,
                    title: "提示",
                    btn: ["确定", "取消"] //按钮
                }, function (index) {
                    tip_index = load();
                    $.ajax({
                        async: true,
                        url: "${ctx}/user/userRecover",
                        type: "post",
                        dataType: "json",
                        data: {
                            id: data.usrId
                        },
                        success: function (data) {
                            layer.close(tip_index);
                            if (data.result) {
                                success(data.info);
                                table.reload("user-page-data");
                            } else {
                                error(data.info);
                            }
                        }
                    });
                    layer.close(index);
                }, function (index) {
                    layer.close(index);
                });
            }
        });

        table.on("toolbar(user-page-data)", function (obj) {
            if (obj.event == "add") {
                layer.open({
                    type: 2,
                    title: "用户新增",
                    content: "${ctx}/user/userSubmitPage",
                    offset: "t",
                    area: ["850px", "700px"],
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        layero.find("iframe").contents().find("#user-front-submit").trigger("click");
                    }
                });
            }
        });
    });
</script>